package org.csu.wuliu.controller;

import com.alibaba.fastjson.JSONObject;
import org.csu.wuliu.domain.LineItem;
import org.csu.wuliu.domain.MainWarehouse;
import org.csu.wuliu.domain.Request;
import org.csu.wuliu.domain.Order;
import org.csu.wuliu.persistence.LineItemMapper;
import org.csu.wuliu.service.CatalogService;
import org.csu.wuliu.service.MainWarehouseService;
import org.csu.wuliu.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/mainWarehouse")
@ResponseBody
public class MainWarehouseController {
    @Autowired
    private MainWarehouseService mainWarehouseService;


    @Autowired
    private OrderService orderService;

    @Autowired
    private LineItemMapper lineItemMapper;
    /*库存量查询
    响应体:
        {
        "currentstorage": 1000,
        "requesting": 1,
        "whid": 1
        }
     */
    @GetMapping("/viewWarehouse")
    public MainWarehouse viewWarehouse(){
        MainWarehouse mainWarehouse = mainWarehouseService.getWarehouse(0);
        return mainWarehouse;
    }

    @GetMapping("/viewRequestList")
    public List<Request> viewRequest(){
        List<Request> requestList = mainWarehouseService.viewRequest();
        return requestList;
    }

    @GetMapping("/viewHistory")
    public List<Request> viewHandle() {
        List<Request> historyList = mainWarehouseService.viewHistory();
        return historyList;
    }


    //传递类型:
//    {
//        "handletype": "well",
//            "result": "1",
//            "RequestID" : 1
//    }

    @PostMapping("/agree")
    public String agree(@RequestBody String jsonString ){
        JSONObject json = JSONObject.parseObject(jsonString);
        Request request = new Request();
        System.out.println(request.getRequestID());
        request.setRequestID(json.getString("RequestID"));
        request.setHandletype(json.getString("handletype"));
        Date date = new Date();
        request.setRequestDate(date);
        request.setResult(json.getString("result"));
        mainWarehouseService.agree(request);
        List<Order> orders = orderService.getOrdersByOrderStatus(0);
        for(Order order: orders){
            List<LineItem> lineItems = lineItemMapper.getLineItemsByOrderId(order.getOrderId());
            for(LineItem lineItem :lineItems){
                if(Integer.parseInt(lineItem.getItemId())== request.getItemID()){
                    order.setStatus(1);
                    orderService.updateOrder(order);
                }
            }
        }

        return "成功";
    }

    @PostMapping("/reject")
    public String reject(@RequestBody String jsonString ){
        JSONObject json = JSONObject.parseObject(jsonString);
        Request request = new Request();
        System.out.println(request.getRequestID());
        request.setRequestID(json.getString("RequestID"));
        request.setHandletype(json.getString("handletype"));
        Date date = new Date();
        request.setRequestDate(date);
        request.setResult(json.getString("result"));
        mainWarehouseService.reject(request);
        return "成功";
    }



}
